/**
 *
 * @author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.io.*;

// TODO: Dodac META do jar'a pozwalajace na uruchamianie przez java -jar

// Main user interface class
public class Processor {

   // Create and initialize JSPLexer and JSPParser.  Invoke parser.file.  If required, generate addons.
   static void process(DataInputStream in, OutputStream out, String className, String packageName, boolean external_include) throws Exception {
      AddOns.clear();
      JSPLexer lexer = new JSPLexer(in);
      JSPParser parser = new JSPParser(lexer);
      parser.converter.init(out);

      if(external_include) {
         parser.converter.generateExternals();
      }

      parser.file();

      if(!external_include) {
         parser.converter.generateAddons();
      }
   }

   //  Parse cmd line parameter, create input and output stream, invoke process.
   public static void main(String[] args) {
      try {
         boolean external_include = false;

         String outputDirName=null;
         int args_len=0;
         for(int i=0; i<args.length; i++) {
            String arg=args[i];
            if(arg.equals("-d")) {
               i++;
               if(i<args.length && outputDirName==null) {
                  outputDirName=args[i];
               }
               else if(i==args.length) {
                  throw new Exception("-d requires argument!");
               }
               else {
                  throw new Exception("Only one -d option is admissible!");
               }
            }
            else if(arg.equals("-external-include")) {
               external_include = true;
            }
            else {
               args_len++;
            }
         }

         if(args_len<1) {
            process(new DataInputStream(System.in), System.out, null, null, external_include);
         }
         else {
            for(int i=0; i<args.length; i++) {
               String inputFileName=args[i];

               if(inputFileName.equals("-d")) {
                  i++;
                  continue;
               }
               else if(inputFileName.equals("-external-include")) {
                  continue;
               }

               int separator=inputFileName.lastIndexOf(File.separator);

               String className;
               if(separator>-1) {
                  className=inputFileName.substring(separator+1);
               }
               else {
                  className=inputFileName;
               }
               if(className.toLowerCase().endsWith(".jsp")) {
                  className=className.substring(0, className.length()-4)+".php";
               }

               String outputFileName;
               if(outputDirName!=null) {
                  outputFileName=outputDirName+File.separator+className;
               }
               else if(separator>-1) {
                  outputFileName=inputFileName.substring(0, separator+1)+className;
               }
               else {
                  outputFileName=className;
               }

               process(new DataInputStream(new FileInputStream(inputFileName)), new FileOutputStream(outputFileName), className, null, external_include);
            }
         }
      } catch(Exception e) {
         System.err.println("Exception: "+e);
         System.exit(1);
      }
   }
}


